home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / rkeyboar.cpt / Reactive Keyboard ƒ / Files.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-22  |  9.8 KB  |  428 lines

  1. #include "MacIncludes.h"
  2. #include "Rk.h"
  3. #include "Structs.h"
  4. #include "Strings.h"
  5.  
  6. extern    WindowPtr    gText;
  7. extern     void         SetCheckBox(dialog,item,state);
  8. extern    Str255        gFileName;
  9.  
  10.  
  11. void DoSave();
  12.  
  13. extern char *pStrcat(char *s,char *t);
  14. extern char *pStrcpy(char *s,char *t);
  15. extern char *PathNameFromWD(long    vRefNum,    char    *s);
  16. extern void EnableDItem(DialogPtr dialog, short item, short state);
  17. extern short GetRealRefNum(long vRefNum);
  18.  
  19. Point            gLocation = {0x40,0x40};
  20. SFReply            reply;
  21. SFTypeList        typeList={kFileType};
  22.  
  23. Boolean            gOpenPrime=true;
  24. Boolean            gOpenClear=false;
  25. Boolean            gNewFile=true;
  26. Boolean            gNone=false;
  27. extern Boolean    gChanged;
  28.  
  29. extern            PrefHandle gPrefs,gOldPrefs;
  30.  
  31.  
  32. #define firstTime        -1  /* the first time our hook is called, it is passed a -1 */
  33.  
  34. #define reDrawList        101 /* returning 101 as item number will cause the FILE list
  35.                                 to be recalculated */
  36.  
  37. #define BTNON            1   /* Control value for on */
  38. #define BTNOFF            0   /* Control value for off */
  39. #define hide            1
  40. #define show            0
  41.  
  42.  
  43. #pragma segment Files
  44. pascal short MySFGetHook(MySFItem, theDialog)
  45.     short        MySFItem;
  46.     DialogPtr    theDialog;
  47. {
  48.  
  49.  
  50.     /* MySFGetHook is a function that requires that an item number be passed    */
  51.     /* back from it. Normally, this is the same item number that was passed        */
  52.     /* to us, but not necessarily. For instance, clicks on the Quit button        */
  53.     /* get translated into clicks on the Cancel button. We could also return    */
  54.     /* values that cause the file names to be redrawn or have the whole event    */
  55.     /* ignored. However, by default, we'll return what was sent to us.            */
  56.  
  57.     switch (MySFItem) {
  58.         case firstTime:
  59.         /* Before the dialog is drawn, our hook gets called with a -1. */
  60.         /* This gives us the opportunity to change things like Button titles, etc. */
  61.  
  62.             gOpenPrime=true;
  63.             gOpenClear=false;
  64.             SetCheckBox(theDialog,iOpenPrimeButton,BTNON);
  65.             EnableDItem(theDialog,iOpenClearButton,show);
  66.             SetCheckBox(theDialog,iOpenClearButton,BTNOFF);
  67.             break;
  68.  
  69.         case iOpenPrimeButton:
  70.  
  71.             if (gOpenPrime) {
  72.                 SetCheckBox(theDialog,iOpenPrimeButton,BTNOFF);
  73.                 EnableDItem(theDialog,iOpenClearButton,hide);
  74.                 SetCheckBox(theDialog,iOpenClearButton,BTNOFF);
  75.                 gOpenClear=false;
  76.                 gOpenPrime=!gOpenPrime;
  77.                 return(iOpenPrimeButton);
  78.             } else {
  79.                 SetCheckBox(theDialog,iOpenPrimeButton,BTNON);
  80.                 EnableDItem(theDialog,iOpenClearButton,show);
  81.                 gOpenPrime=!gOpenPrime;
  82.                 return(iOpenPrimeButton);
  83.             }
  84.  
  85.             
  86.             break;
  87.  
  88.         case iOpenClearButton:
  89.  
  90.             if (gOpenClear) {
  91.                 SetCheckBox(theDialog,iOpenClearButton,BTNOFF);
  92.                 gOpenClear = false;
  93.                 return(iOpenClearButton); 
  94.             }else {
  95.                 SetCheckBox(theDialog,iOpenClearButton,BTNON);
  96.                 gOpenClear = true;
  97.                 return(iOpenClearButton); 
  98.             }
  99.             break;
  100.  
  101.         default:
  102.             return(MySFItem);
  103.     }
  104. }
  105.  
  106.  
  107. void DoOpen()
  108. {
  109.     short    thefile;
  110.     Ptr        TEText;
  111.     int        size;
  112.     int     err;
  113.     int        x;
  114.     Str255    s;
  115.  
  116.     SFPGetFile(gLocation,    /* location */
  117.         "\pString",    /* vestigial string */
  118.         nil,    /* fileFilter */
  119.         1,
  120.         &typeList,            /* array to types to show */
  121.         MySFGetHook,        /* dlgHook */
  122.         &reply,                /* record for returned values */
  123.         rSFPGetFileDLOG,    /* ID of Custom Dialog */
  124.         nil);                /* ModalDialog filterProc */
  125.     if(reply.good){
  126.         if(gOpenPrime){
  127.             if(PathNameFromWD(reply.vRefNum,&s) != -1){
  128.                 PrimeFromFile(p2cstr(pStrcat(s,reply.fName)),gOpenClear);
  129.             }else
  130.                 AlertUser(eCantPrime);
  131.         }
  132.         err=FSOpen(reply.fName,reply.vRefNum,&thefile);
  133.         if(err){
  134.             FileSystemError(reply.fName,"\popen",err,false);
  135.             return;
  136.         }
  137.         err=GetEOF(thefile,&size);
  138.         if(err){
  139.             FileSystemError(reply.fName,"\pfind size of",err,false);
  140.             return;
  141.         }
  142.         if(size>kMaxTELength){
  143.             AlertUser(eExceedChar);
  144.             FSClose(thefile);
  145.             return;
  146.         }
  147.         TEText=NewPtr(size);
  148.         if(TEText==nil){
  149.             AlertUser(eNoMemory);
  150.             FSClose(thefile);
  151.             return;
  152.         }
  153.         DoNew();
  154.         if(gText){
  155.             err=FSRead(thefile,&size,TEText);
  156.             if(err){
  157.                 FileSystemError(reply.fName,"\pfind size of",err,false);
  158.                 return;
  159.             }
  160.             TESetText(TEText,size,((DocumentPeek)gText)->docTE);
  161.         }
  162.         DisposPtr(TEText);
  163.         err=FSClose(thefile);
  164.         if(err){
  165.             FileSystemError(reply.fName,"\pfind size of",err,false);
  166.             return;
  167.         }
  168.         TESetSelect(0,0,((DocumentPeek)gText)->docTE);
  169.         AdjustScrollbars(gText,false);
  170.         SetWTitle(gText,reply.fName);
  171.         gNewFile=false;
  172.         for(x=0;x<=reply.fName[0];x++)
  173.             gFileName[x]=reply.fName[x];
  174.         gChanged = false;
  175.     }
  176. }
  177.  
  178. pascal short MySFPrimeHook(MySFItem, theDialog)
  179.     short        MySFItem;
  180.     DialogPtr    theDialog;
  181. {
  182.  
  183.  
  184.     /* MySFPrimeHook is a function that requires that an item number be passed    */
  185.     /* back from it. Normally, this is the same item number that was passed        */
  186.     /* to us, but not necessarily. For instance, clicks on the Quit button        */
  187.     /* get translated into clicks on the Cancel button. We could also return    */
  188.     /* values that cause the file names to be redrawn or have the whole event    */
  189.     /* ignored. However, by default, we'll return what was sent to us.            */
  190.  
  191.     switch (MySFItem) {
  192.         case firstTime:
  193.         /* Before the dialog is drawn, our hook gets called with a -1. */
  194.         /* This gives us the opportunity to change things like Button titles, etc. */
  195.  
  196.             gOpenPrime=true;
  197.             gOpenClear=false;
  198.             EnableDItem(theDialog,iOpenPrimeButton,hide);
  199.             SetCheckBox(theDialog,iOpenClearButton,BTNOFF);
  200.             break;
  201.  
  202.  
  203.         case iOpenClearButton:
  204.  
  205.             if (gOpenClear) {
  206.                 SetCheckBox(theDialog,iOpenClearButton,BTNOFF);
  207.                 gOpenClear = false;
  208.                 return(iOpenClearButton); 
  209.             }else {
  210.                 SetCheckBox(theDialog,iOpenClearButton,BTNON);
  211.                 gOpenClear = true;
  212.                 return(iOpenClearButton); 
  213.             }
  214.             break;
  215.  
  216.         default:
  217.             return(MySFItem);
  218.     }
  219. }
  220.  
  221. void DoPrime()
  222. {
  223.     Str255    s;
  224.  
  225.     SFPGetFile(gLocation,    /* location */
  226.         "\pString",    /* vestigial string */
  227.         nil,    /* fileFilter */
  228.         1,
  229.         &typeList,            /* array to types to show */
  230.         MySFPrimeHook,        /* dlgHook */
  231.         &reply,                /* record for returned values */
  232.         rSFPGetFileDLOG,    /* ID of Custom Dialog */
  233.         nil);                /* ModalDialog filterProc */
  234.     if(reply.good){
  235.         if(PathNameFromWD(reply.vRefNum,&s) != -1){
  236.             PrimeFromFile(p2cstr(pStrcat(s,reply.fName)),gOpenClear);
  237.         }else
  238.             AlertUser(eCantPrime);
  239.     }
  240.     DrawNewPredictions();
  241. }
  242.  
  243. void DoSaveAs(Str255 name)
  244. {
  245.     int x;
  246.  
  247.     SFPutFile(gLocation,            /* location */
  248.         "\pSave document as:",        /* prompt string */
  249.         name,                        /* original name */
  250.         nil,                        /* dlgHook */
  251.         &reply);                    /* record for returned values */
  252.  
  253.     if (reply.good){
  254.         gNewFile=false;
  255.         DoSave();
  256.         for(x=0;x<=reply.fName[0];x++)
  257.             gFileName[x]=reply.fName[x];
  258.         SetWTitle(gText,reply.fName);
  259.     }
  260. }
  261.  
  262. void DoSave()
  263. {
  264.     short thefile;
  265.     int err;
  266.     CharsHandle TEText;
  267.     int size;
  268.     
  269.     if(gNewFile){
  270.         DoSaveAs(gFileName);
  271.     }
  272.         err=FSOpen(reply.fName,reply.vRefNum,&thefile);
  273.         if(err==fnfErr){
  274.             err=Create(reply.fName,reply.vRefNum,kCreator,kFileType);
  275.             if(err){
  276.                 FileSystemError(reply.fName,"\pcreate",err,false);
  277.                 return;
  278.             }
  279.             err=FSOpen(reply.fName,reply.vRefNum,&thefile);
  280.             if(err){
  281.                 FileSystemError(reply.fName,"\popen",err,false);
  282.                 return;
  283.             }
  284.         }else{
  285.             if(err){
  286.                 FileSystemError(reply.fName,"\popen",err,false);
  287.                 return;
  288.             }
  289.         }
  290.         err=SetEOF(thefile,0);
  291.         if(err){
  292.             FileSystemError(reply.fName,"\psave",err,false);
  293.             return;
  294.         }
  295.         TEText=TEGetText(((DocumentPeek)gText)->docTE);
  296.         HLock((Handle)TEText);
  297.         size=(long)GetHandleSize((Handle)TEText);
  298.         err=FSWrite(thefile,&size,(Ptr)*TEText);
  299.         if(err){
  300.             FileSystemError(reply.fName,"\pwrite",err,false);
  301.             return;
  302.         }
  303.         err=FSClose(thefile);
  304.         if(err){
  305.             FileSystemError(reply.fName,"\pfind size of",err,false);
  306.             return;
  307.         }
  308.         gNewFile=false;
  309.         gChanged = false;
  310. }
  311.  
  312. void DoRevert()
  313. {
  314.     short        itemHit;
  315.  
  316.     short    thefile;
  317.     Ptr        TEText;
  318.     int        size;
  319.     int     err;
  320.  
  321.     SetCursor(&qd.arrow);
  322.     ParamText(gFileName, "", "", "");
  323.     
  324.     itemHit = Alert(rRevertAlert, nil);
  325.  
  326.     if(itemHit == iOk){
  327.         err=FSOpen(reply.fName,reply.vRefNum,&thefile);
  328.         if(err){
  329.             FileSystemError(reply.fName,"\popen",err,false);
  330.             return;
  331.         }
  332.         err=GetEOF(thefile,&size);
  333.         if(err){
  334.             FileSystemError(reply.fName,"\pfind size of",err,false);
  335.             return;
  336.         }
  337.         if(size>kMaxTELength){
  338.             AlertUser(eExceedChar);
  339.             FSClose(thefile);
  340.             return;
  341.         }
  342.         TEText=NewPtr(size);
  343.         if(TEText==nil){
  344.             AlertUser(eNoMemory);
  345.             FSClose(thefile);
  346.             return;
  347.         }
  348.         err=FSRead(thefile,&size,TEText);
  349.         if(err){
  350.             FileSystemError(reply.fName,"\pfind size of",err,false);
  351.             return;
  352.         }
  353.         TESetText(TEText,size,((DocumentPeek)gText)->docTE);
  354.         DisposPtr(TEText);
  355.         err=FSClose(thefile);
  356.         if(err){
  357.             FileSystemError(reply.fName,"\pclose",err,false);
  358.             return;
  359.         }
  360.         TESetSelect(0,0,((DocumentPeek)gText)->docTE);
  361.         AdjustScrollbars(gText,false);
  362.         DrawWindow(gText);
  363.         DrawNewPredictions();
  364.     }        
  365. }
  366.  
  367. pascal short MySFNoneHook(MySFItem, theDialog)
  368.     short        MySFItem;
  369.     DialogPtr    theDialog;
  370. {
  371. #pragma unused (theDialog)
  372.     switch (MySFItem) {
  373.         case firstTime:
  374.         /* Before the dialog is drawn, our hook gets called with a -1. */
  375.         /* This gives us the opportunity to change things like Button titles, etc. */
  376.             gNone=false;
  377.             break;
  378.  
  379.  
  380.         case iNoneButton:
  381.             gNone=true;
  382.             return(iCancel);
  383.             break;
  384.  
  385.         default:
  386.             return(MySFItem);
  387.     }
  388. }
  389.  
  390.  
  391. GetPrimeFile(Boolean PrimeOrZero, PrefHandle newPrefs)
  392. {
  393.  
  394.     SFPGetFile(gLocation,    /* location */
  395.         "\pString",    /* vestigial string */
  396.         nil,    /* fileFilter */
  397.         1,
  398.         &typeList,            /* array to types to show */
  399.         MySFNoneHook,        /* dlgHook */
  400.         &reply,                /* record for returned values */
  401.         rGetFileNoneDLOG,    /* ID of Custom Dialog */
  402.         nil);                /* ModalDialog filterProc */
  403.     if(reply.good){
  404.         if(PrimeOrZero){
  405.             (*newPrefs)->pHasPrime=true;
  406.             pStrcpy((*newPrefs)->pPrimeName,reply.fName);
  407.             (*newPrefs)->pPrimeDirID=GetDirID(reply.vRefNum);
  408.             (*newPrefs)->pPrimeRefNum=GetRealRefNum(reply.vRefNum);
  409.         } else {
  410.             (*newPrefs)->pHasZero=true;
  411.             pStrcpy((*newPrefs)->pZeroName,reply.fName);
  412.             (*newPrefs)->pZeroDirID=GetDirID(reply.vRefNum);
  413.             (*newPrefs)->pZeroRefNum=GetRealRefNum(reply.vRefNum);
  414.         }
  415.         return(true);
  416.     }else{
  417.         if(gNone) {
  418.             if(PrimeOrZero){
  419.                 (*newPrefs)->pHasPrime=false;
  420.             } else {
  421.                 (*newPrefs)->pHasZero=false;
  422.             }
  423.             return(true);
  424.         }
  425.         return(false);
  426.     }
  427. }
  428.